package com.hyzx.qbasic.admin.controller.demo;

import com.hyzx.qbasic.admin.controller.BaseController;
import com.hyzx.qbasic.admin.model.po.AdminUserInfoPO;
import com.hyzx.qbasic.admin.model.po.SysParamPO;
import com.hyzx.qbasic.admin.model.vo.req.admin.GetParamListReq;
import com.hyzx.qbasic.admin.model.vo.req.admin.GetUserListReq;
import com.hyzx.qbasic.admin.domain.service.IAdminUserService;
import com.hyzx.qbasic.admin.domain.service.IPoiService;
import com.hyzx.qbasic.admin.domain.service.ISysParamService;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 黄江华 on 2018/8/20
 * 示例代码
 */

@Controller
@RequestMapping("/demo")
public class DemoController extends BaseController {
    @Autowired
    private IPoiService poiService;

    @Autowired
    private ISysParamService sysParamService;

    @Autowired
    private IAdminUserService userService;

    @GetMapping("/exportOfficeDoc")
    public String exportOfficeDoc() {
        return "demo/exportOfficeDoc";
    }

    @GetMapping("/exportWord")
    public void exportWord(HttpServletResponse response) {
        //模板文件路径
        String templateFileUrl = "templates/demo/exportWordDemo.doc";
        //导出文件名
        String fileName = "导出Word文档示例";
        //构造替换模板内容
        Map<String, String> contentMap = new HashMap<>();
        contentMap.put("applyTime", "2018年8月27日");
        contentMap.put("company", "XXX有限公司");
        contentMap.put("startTime", "2016年1月1日");
        contentMap.put("unicode", "123456");
        contentMap.put("capital", "500万");
        contentMap.put("address", "人民东路53号");
        contentMap.put("website", "www.test.com");
        contentMap.put("name", "张三");
        contentMap.put("tel", "0991-123456");
        contentMap.put("phone", "186111112222");
        contentMap.put("email", "test@163.com");

        // 导出word文档
        poiService.exportWordToResponse(templateFileUrl, fileName, contentMap, response);
    }

    @GetMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response) {
        //模板文件路径
        String templateFileUrl = "templates/demo/exportExcelDemo.xls";
        //导出文件名
        String fileName = "导出Excel文档示例";

        //导出到第一个工作表
        int sheetIndex = 0;
        // 第一个工作表模板表头所占行数
        int headerRows = 2;
        // 第一个工作表模板每列数据字段名
        List<String> cols = Arrays.asList("pType", "pKey", "pValue", "sort", "remark");

        // 获取第一个工作表要导出的数据
        GetParamListReq req = new GetParamListReq();
        req.setPageNum(1);
        req.setPageSize(100);
        List<SysParamPO> data = sysParamService.getPagedParamList(req).getList();

        //导出到第二个工作表
        int sheetIndex2 = 1;
        // 第二个工作表模板表头所占行数
        int headerRows2 = 2;
        // 第二个工作表模板每列数据字段名
        List<String> cols2 = Arrays.asList("userId", "userName", "realName", "createTime");

        // 获取第二个工作表要导出的数据
        GetUserListReq req2 = new GetUserListReq();
        req2.setPageNum(1);
        req2.setPageSize(100);
        List<AdminUserInfoPO> data2 = userService.getPagedUserList(req2).getList();

        //创建Excel工作簿对象
        HSSFWorkbook workbook = poiService.getWorkbook(templateFileUrl);
        if (workbook == null) {
            return;
        }

        //导出Excel文档到第一个工作表
        poiService.exportEntityToExcel(workbook, sheetIndex, cols, headerRows, data);
        //导出Excel文档到第二个工作表
        poiService.exportEntityToExcel(workbook, sheetIndex2, cols2, headerRows2, data2);
        //输出响应
        poiService.exportExcelToResponse(workbook, fileName, response);
    }
}
